<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

class CouponsComponent
{
	function CouponsComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else if ($action == 'ToggleStatus') 
			return $this->toggleStatus($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];

		$sqlQuery = 'select coupon_id, coupon_code, coupon_amount, coupon_type, coupon_status, coupon_use  ' .
		'from ' . TABLE_COUPONS . ' ' .
		'order by coupon_code';
		if ($paginate) {
			$sqlQuery .= ' limit ' . $offset . ', ' . $max_rows;
		}
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['coupon_id'];
			$value{$handle}{$i}{'code'}= $row['coupon_code'];
			$value{$handle}{$i}{'amount'}= (double)$row['coupon_amount'];
			$value{$handle}{$i}{'type'}= (int)$row['coupon_type'];
			$value{$handle}{$i}{'status'}= (boolean)$row['coupon_status'];
			$value{$handle}{$i}{'use'}= (int)$row['coupon_use'];
			$i++;
		}

		if ($paginate) {
			$sqlQuery = 'select count(coupon_id) as cnt from ' . TABLE_COUPONS;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt = (int)$row['cnt'];
			}

			$value{$handle . 'MetaData'}{'max_rows'}= (int)$max_rows;
			$value{$handle . 'MetaData'}{'offset'}= $offset;
			$value{$handle . 'MetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{$handle . 'MetaData'}{'rows'}= $i;
			$value{$handle . 'MetaData'}{'total_rows'}= $cnt;
		}

		return $value;
	}

	function edit(&$params) 
	{
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 1000) break;

			$couponId = (int)$params['couponId-'.$i];
			if ($couponId == 0) break;

			$couponCode = prepare_input($params['couponCode-'.$i]);
			if ($couponCode == null || strlen($couponCode) == 0) continue;
			
			$couponAmount = (double)$params['couponAmount-'.$i];
			$couponType = (int)$params['couponType-'.$i];
			$couponStatus = (boolean)$params['couponStatus-'.$i];
			$couponUse = (int)$params['couponUse-'.$i];

			$deleteRow = (boolean)$params['couponDelete-'.$i];

			if ($deleteRow) {
				if ($couponId < 1) continue;
				$sql = 'delete from ' . TABLE_COUPONS . ' where coupon_id=' . $couponId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sql_data_array = array();
				$sqlAction = 'insert';
				$where = '';
				if ($couponId > 0) {
					$sqlAction = 'update';
					$where = 'coupon_id=' . $couponId;
				}
				$sql_data_array['coupon_code'] = $couponCode;
				$sql_data_array['coupon_amount'] = $couponAmount;
				$sql_data_array['coupon_type'] = $couponType;
				$sql_data_array['coupon_status'] = $couponStatus;
				$sql_data_array['coupon_use'] = $couponUse;
				db_perform(TABLE_COUPONS, $sql_data_array, $sqlAction, $where);
			}
		}

		return $this->query($params);
	}

	function toggleStatus(&$params) 
	{
		$couponId = (int)$params['couponId'];
		$sqlQuery = 'select coupon_status from ' . TABLE_COUPONS . ' where coupon_id=' . $couponId;
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		
		while($row = mysql_fetch_array($dataReturned))
		{
			$status = (int)$row['coupon_status'];
			if ($status != 0) $status = 0;
			else $status = 1;
			$sql = 'update ' . TABLE_COUPONS . ' set coupon_status=' . $status . ' where coupon_id=' . $couponId;
			mysql_query($sql) or die(mysql_error());
			break;
		}
		return $this->query($params);
	}
}
?>
